<%
layout("/layouts/platform.html"){
%>
<header class="header navbar bg-white shadow">
    <!-- 左按钮区 -->
    <!-- <div class="btn-group tool-button">
        <a class="btn btn-primary navbar-btn" href="${base}/platform/losys/logisticsGroup" data-pjax id="goback"><i class="ti-angle-left"></i> 返回</a>
    </div> -->
    <div class="pull-right">
        <div class="btn-group tool-button">
            <button class="btn btn-primary navbar-btn" onclick="complete()"><i class="ti-share"></i> 设置区域价格</button>
        </div>
    </div>
</header>
<div class="content-wrap">
    <div class="wrapper" style="min-height: 500px;">
        <div class="row mb15">
            <div class="col-lg-12">
                <form id="addRoleForm" class="form-horizontal stepy" method="post" action="${base}/platform/losys/areaPrice">
                    <div class="form-group no-b">
                        <div class="col-sm-8">
                            <div class="panel">
                            	<br>
                            	<div class="form-group">
	                            	<label for="name" class="col-sm-2 control-label">选择公司</label>
	
	                            	<div class="col-sm-8">
	                                <select id="logisticsId" name="logisticsId" class="form-control" onchange="search()">
						        		<% for(logistic in logistics){ %>
						        		    <% if(logisticsId == logistic.id) {%>
										     <option value=${logistic.id!} selected="selected">${logistic.name!}</option>
										    <%}else if(logisticsId != logistic.id) {%>  
										     <option value=${logistic.id!} >${logistic.name!}</option>
										    <%}%>
										<%}%>	
						        	</select>
                            		</div>
                        		</div>
                                <table id="treeTable" class="table no-m">
				                    <thead>
				                    <tr>
				                        <th class="col-md-2 pd-l-lg">
				                            <span class="pd-l-sm ml20"></span>区域名称
				                        </th>
				                        <th class="col-md-1">区域价格组名称</th>
				                    </tr>
				                    </thead>
				                    <tbody>
				                    <% for(obj in list){ %>
				                    <tr data-tt-id="${obj.id!}" data-tt-parent-id="${obj.pid!}" data-tt-branch="${obj.haschild==1}">
				                        <td>
				                            <input type="checkbox" class="shuibian" value="${obj.id!}"><span class="pd-l-sm"></span><i class=""></i> ${obj.name!}
				                        </td>
				                        <td>
				                        ${obj.price!}
				                        </td>
				                    </tr>
				                    <%} %>
				                    </tbody>
				                </table>
                            </div>
                        </div>
                        <div class="col-sm-4">
                            <div class="panel">
                                <div class="panel-heading no-b">
                                    <h5><b>描述</b></h5>
                                </div>
                                <div id="menuDescript" class="panel-body">
                                    <div class='alert alert-info'>请勾选区域</div>
                                </div>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<script language="JavaScript">
var treeTable,currid=[],getChildren,reset;
$(function () {
	refreshCheckEvent();
    $("#dialogDetail").on("hidden.bs.modal", function() {
        $(this).removeData("bs.modal");
    });
    $("#dialogSort").on("hidden.bs.modal", function() {
        $(this).removeData("bs.modal");
    });
    treeTable= $("#treeTable").treetable({
        expandable: true,
        onNodeExpand : function(){
            /*var node = this;
            var rows = "<tr data-tt-id=\"loading\" data-tt-parent-id=\""+node.id+"\" data-tt-branch=\"false\"><td colspan='5'><img src=\""+base+"/assets/img/loading.gif\"/>loading</td></tr>";
            treeTable.treetable("loadBranch", node, rows);
            var logisticsId = $("#logisticsId").val();
            if(logisticsId==""){
            	logisticsId="0";
            }
            $.post("${base}/platform/losys/areaPrice/child/"+node.id+"/"+logisticsId,{}, function(data){
                treeTable.treetable("unloadBranch", node);
                treeTable.treetable("loadBranch", node, data);
                refreshCheckEvent();
            });*/
        },
        onNodeInitialized:function(){
        }
    });
});
    function each(node, tree, nodes) {
        var p = tree.get_parent(node);
        if (p.length > 1) {
            if (nodes&&p&&nodes.indexOf(p)<0)
                nodes.push(p);
            each(p, tree, nodes);
        }
    }
    function complete() {
    	var logisticsId = $("#logisticsId").val();
    	if(logisticsId.trim()==""){
    		Toast.warning("请选择物流公司");
    		return
    	}
    	var num="";
    	$.each($('input:checkbox:checked'),function(){
    		num+=$(this).val()+",";
    	});
    	num=num.substring(0,num.length-1);

        if (num.length > 0) {
          /*  $.post("${base}/platform/losys/logisticsGroup/setPrice", {logistics: ids.toString(), groupName: groupName});  */
       window.location.href = "${base}/platform/losys/areaPrice/setPrice/"+num.toString()+"/" + logisticsId;
    } else {
        Toast.warning("请先选择区域！");
    }
    }
    
    function search() {
    	if($("#logisticsId").val()!=""){
    		window.location.href ="${base}/platform/losys/areaPrice/data/"+$("#logisticsId").val();
    	}
	}
    
    function refreshCheckEvent(){
    	$(".shuibian").unbind();
        $(".shuibian").change(function() {
        	var row=$(this).parent().parent()
        	var hasChildren=row.data('tt-branch');
        	var id=row.data('tt-id');
        	var ischecked=$(this).is(':checked');
        	if(hasChildren){
        		//console.log("expandNode", id);
        		//$("#treeTable").treetable("expandNode", id);
        	}
        	updateCheck(id,ischecked);
       	});
    }
    
    function updateCheck(preParentId,checked){
    	if(preParentId){
			var cn=$('[data-tt-parent-id='+preParentId+']');
   			var cid=0;
   			var hasChildren=false;
   			$.each(cn,function(index,item) {
   				hasChildren=$(item).data('tt-branch');
   				
   				cid=$(item).data('tt-id');

				$(item).find('input').prop('checked',checked);
				
				if(hasChildren){
					updateCheck(cid,checked);
   	   			}
   				
   			});
		}
    }
    
    function getAllChildrenNode(preParentId,checked){
		if(preParentId){
			var cn=$('[data-tt-parent-id='+preParentId+']');
   			var cid=cn.data('tt-id');
   			var hasChildren=false;
   			$.each(cn,function(index,item) {
   				hasChildren=$(item).data('tt-branch');
   				if(checked){
   					$(item).find('input').attr('checked',checked);
   					if(hasChildren){
   	   					$('[data-tt-id='+cid+'] input').change();
   	   	   			}
   				}
   				
   			});
		}
    }
</script>
<%}%>